package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.OnlineModel;
import com.zeng.jiudian.bean.OnlineModelExample;
import com.zeng.jiudian.bean.OnlineModelExample.Criteria;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.dao.OnlineDao;
import com.zeng.jiudian.mapper.OnlineMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/30 - 17:50
 */
public class OnlineDaoImpl implements OnlineDao
{
    private SqlSession sqlSession;

    @Override
    public int add(OnlineModel onlineModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        onlineModel.setCreateTime(dateFormat.format(new Date()));
        onlineModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            OnlineMapper mapper = sqlSession.getMapper(OnlineMapper.class);
            count = mapper.insert(onlineModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<OnlineModel> findAll(OnlineModel onlineModel)
    {
        List<OnlineModel> list = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            OnlineMapper mapper = sqlSession.getMapper(OnlineMapper.class);
            OnlineModelExample example = new OnlineModelExample();
            Criteria criteria = example.createCriteria();
            if (!"".equals(onlineModel.getName()) && onlineModel.getName() != null)
            {
                criteria.andNameLike("%" + onlineModel.getName() + "%");
            }
            if (!"".equals(onlineModel.getPhone()) && onlineModel.getPhone() != null)
            {
                criteria.andPhoneLike("%" + onlineModel.getPhone() + "%");
            }
            if (!"全部".equals(onlineModel.getRoomtype()) && onlineModel.getRoomtype() != null)
            {
                criteria.andRoomtypeLike("%" + onlineModel.getRoomtype() + "%");
            }
            if (!"".equals(onlineModel.getIdea()) && onlineModel.getIdea() != null)
            {
                criteria.andIdeaLike("%" + onlineModel.getIdea() + "%");
            }
            if (!"".equals(onlineModel.getBeginStartDate()) && onlineModel.getBeginStartDate() != null && !"".equals(onlineModel.getEndStartDate()) && onlineModel.getEndStartDate() != null)
            {
                criteria.andStartdateBetween(onlineModel.getBeginStartDate(), onlineModel.getEndStartDate());
            }
            else
            {
                if (!"".equals(onlineModel.getBeginStartDate()) && onlineModel.getBeginStartDate() != null)
                {
                    criteria.andStartdateGreaterThanOrEqualTo(onlineModel.getBeginStartDate());
                }
                else if (!"".equals(onlineModel.getEndStartDate()) && onlineModel.getEndStartDate() != null)
                {
                    criteria.andStartdateLessThanOrEqualTo(onlineModel.getEndStartDate());
                }
            }
            if (!"".equals(onlineModel.getBeginEndDate()) && onlineModel.getBeginEndDate() != null && !"".equals(onlineModel.getEndEndDate()) && onlineModel.getEndEndDate() != null)
            {
                criteria.andEnddateBetween(onlineModel.getBeginEndDate(), onlineModel.getEndEndDate());
            }
            else
            {
                if (!"".equals(onlineModel.getBeginEndDate()) && onlineModel.getBeginEndDate() != null)
                {
                    criteria.andEnddateGreaterThanOrEqualTo(onlineModel.getBeginEndDate());
                }
                else if (!"".equals(onlineModel.getEndEndDate()) && onlineModel.getEndEndDate() != null)
                {
                    criteria.andEnddateLessThanOrEqualTo(onlineModel.getEndEndDate());
                }
            }
            if (!"无".equals(onlineModel.getOrder()) && onlineModel.getOrder() != null)
            {
                if ("入住时间".equals(onlineModel.getOrder()))
                {
                    if ("降序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("startDate desc");
                    }
                    else if ("升序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("startDate asc");
                    }
                }
                else if ("退房时间".equals(onlineModel.getOrder()))
                {
                    if ("降序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("endDate desc");
                    }
                    else if ("升序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("endDate asc");
                    }
                }
                else if ("创建时间".equals(onlineModel.getOrder()))
                {
                    if ("降序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("create_time desc");
                    }
                    else if ("升序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("create_time asc");
                    }
                }
                else if ("更新时间".equals(onlineModel.getOrder()))
                {
                    if ("降序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("update_time desc");
                    }
                    else if ("升序".equals(onlineModel.getLift()))
                    {
                        example.setOrderByClause("update_time asc");
                    }
                }
            }
            else
            {
                if ("降序".equals(onlineModel.getLift()))
                {
                    example.setOrderByClause("id desc");
                }
                else if ("升序".equals(onlineModel.getLift()))
                {
                    example.setOrderByClause("id asc");
                }
            }
            PageModel pageModel = onlineModel.getPageModel();
            if (pageModel != null)
            {
                Integer nowNum = pageModel.getNowNum();
                Integer pageSize = pageModel.getPageSize();
                if (nowNum != null && pageSize != 0 && pageSize != null)
                {
                    example.setStartRow((nowNum - 1) * pageSize);
                    example.setPageSize(pageSize);
                }
            }
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int getCount(OnlineModel onlineModel)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            OnlineMapper mapper = sqlSession.getMapper(OnlineMapper.class);
            OnlineModelExample example = new OnlineModelExample();
            Criteria criteria = example.createCriteria();
            if (!"".equals(onlineModel.getName()) && onlineModel.getName() != null)
            {
                criteria.andNameLike("%" + onlineModel.getName() + "%");
            }
            if (!"".equals(onlineModel.getPhone()) && onlineModel.getPhone() != null)
            {
                criteria.andPhoneLike("%" + onlineModel.getPhone() + "%");
            }
            if (!"全部".equals(onlineModel.getRoomtype()) && onlineModel.getRoomtype() != null)
            {
                criteria.andRoomtypeLike("%" + onlineModel.getRoomtype() + "%");
            }
            if (!"".equals(onlineModel.getIdea()) && onlineModel.getIdea() != null)
            {
                criteria.andIdeaLike("%" + onlineModel.getIdea() + "%");
            }
            if (!"".equals(onlineModel.getBeginStartDate()) && onlineModel.getBeginStartDate() != null && !"".equals(onlineModel.getEndStartDate()) && onlineModel.getEndStartDate() != null)
            {
                criteria.andStartdateBetween(onlineModel.getBeginStartDate(), onlineModel.getEndStartDate());
            }
            else
            {
                if (!"".equals(onlineModel.getBeginStartDate()) && onlineModel.getBeginStartDate() != null)
                {
                    criteria.andStartdateGreaterThanOrEqualTo(onlineModel.getBeginStartDate());
                }
                else if (!"".equals(onlineModel.getEndStartDate()) && onlineModel.getEndStartDate() != null)
                {
                    criteria.andStartdateLessThanOrEqualTo(onlineModel.getEndStartDate());
                }
            }
            if (!"".equals(onlineModel.getBeginEndDate()) && onlineModel.getBeginEndDate() != null && !"".equals(onlineModel.getEndEndDate()) && onlineModel.getEndEndDate() != null)
            {
                criteria.andEnddateBetween(onlineModel.getBeginEndDate(), onlineModel.getEndEndDate());
            }
            else
            {
                if (!"".equals(onlineModel.getBeginEndDate()) && onlineModel.getBeginEndDate() != null)
                {
                    criteria.andEnddateGreaterThanOrEqualTo(onlineModel.getBeginEndDate());
                }
                else if (!"".equals(onlineModel.getEndEndDate()) && onlineModel.getEndEndDate() != null)
                {
                    criteria.andEnddateLessThanOrEqualTo(onlineModel.getEndEndDate());
                }
            }
            count = (int) mapper.countByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int delete(int id)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            OnlineMapper mapper = sqlSession.getMapper(OnlineMapper.class);
            count = mapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public OnlineModel findById(int id)
    {
        OnlineModel model = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            OnlineMapper mapper = sqlSession.getMapper(OnlineMapper.class);
            model = mapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return model;
    }

    @Override
    public int update(OnlineModel onlineModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        onlineModel.setUpdateTime(dateFormat.format(new Date()));

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            OnlineMapper mapper = sqlSession.getMapper(OnlineMapper.class);
            count = mapper.updateByPrimaryKeySelective(onlineModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }
}
